<?

//Initiate Authenticate
SecurityRedirect ();

//initiate Vars 
define("PRIMARY_KEY","lpj_id");
$KEY[]         = array("PRIMARY_KEY"=>PRIMARY_KEY);
$FileName      = "ais_transaction_lpj.php";
$FileTemplate  = "ais_transaction_lpj_list.html";
$TableName     = "tbllpj";
$SubHeaderTitle= "Pertanggung Jawaban BS";
$PageSize      = 10 ;
$HTMLGridList  = "ToolGrid";
$created_by     = $_SESSION['AISUser'];
$group_login     = $_SESSION['AISGroupName'];




//************ FORM ACTION **************
	if (GetParam("Action","")=="Delete") {
		DeleteRecord ("txtCheck",$PageSize);
	}

//Initiate Database Table For Search;
$jo_code = GetParam("jo_code","");
$projName = GetParam("projName","");
$blNo = GetParam("blNo","");
$vessel_search = GetParam("vessel_search","");
$userName = array();


$SQLGetUserType = "SELECT * FROM $DBGeneral.tbluser as a
                   LEFT JOIN tblusergroup as b ON a.user_id = b.user_id
                   LEFT JOIN $DBGeneral.tblstaff as c ON a.user_id = c.user_id
                   WHERE a.user = '".$created_by."' ";
$ResUserType = $DBConnection->dbc->get_results($SQLGetUserType);


$user_group = $ResUserType[0]->group_id;
$user_type = $ResUserType[0]->user_type;
$user_staff_id = $ResUserType[0]->staff_id;

$SQLGetUser = "SELECT * FROM tblusergroup as a
			   LEFT JOIN $DBGeneral.tbluser as b ON a.user_id = b.user_id
			   LEFT JOIN $DBGeneral.tblstaff as c ON a.user_id = c.user_id
			   WHERE a.group_id = '".$user_group."' ";
			   
$ResUser = $DBConnection->dbc->get_results($SQLGetUser);

foreach($ResUser as $idx => $val)
{	
	if(!empty($val->staff_id))
	{
		$userName[] = $val->staff_id;
	}
}

$countUserName = count($userName);
$itCount = 1;


//******* DATABASE FORM QUERY ***********

if($created_by == "master" || $group_login == "ADMIN")
{
    $conditionBase = " 1 ";
    $conditionBaseAnd = " 1 AND ";
}

else 
{
	if($user_type == "ADMIN")
	{
		if(!empty($userName))
		{
			$conditionBase = " 1 AND ( ";
			$conditionBaseAnd = " 1 AND ( ";
			
			foreach($userName as $idx => $val)
			{
			   
			   $conditionBase.= " a.staff_id = '".$val."' ";
			   if($itCount < $countUserName)
			   {
				   $conditionBase.= " OR ";
			   }
			   
			   $conditionBaseAnd.= " a.staff_id = '".$val."' ";
			   if($itCount < $countUserName)
			   {
				   $conditionBaseAnd.= " OR ";
			   }
			   			   			   
			   $itCount++;
			}
			$conditionBase.= " ) ";
			$conditionBaseAnd.= " ) ";
			$conditionBaseAnd.= " AND ";
	    }
	    
	    else
	    {
		    $conditionBase = " 1 ";
            $conditionBaseAnd = " 1 AND ";
		}
	}
	
	else
	{
		$conditionBase = " a.staff_id = '".$user_staff_id."' ";
		$conditionBaseAnd = " a.staff_id = '".$user_staff_id."' AND ";	
	}
}


$SQL = "SELECT a.*,b.*,c.*,d.*,e.* FROM `$TableName` as a ";
$SQL.= "JOIN tbljoborder as b ON a.jo_id = b.jo_id ";
$SQL.= "JOIN tblproject as c ON b.project_id = c.project_id "; 
$SQL.= "JOIN ".$DBGeneral.".tblstaff as d ON b.staff_id = d.staff_id ";
$SQL.= "JOIN tbldebtur as e ON b.debtur = e.CUCODE ";


if ($jo_code) 
	if (strlen($WHERE)) $WHERE .= " AND "." a.jo_code like '%".$jo_code."%'";
	else $WHERE .= " a.jo_code like '%".$jo_code."%'";

if ($projName) 
	if (strlen($WHERE)) $WHERE .= " AND "." b.project_name like '%".$projName."%'";
	else $WHERE .= " b.project_name like '%".$projName."%'";
	
if ($blNo) 
	if (strlen($WHERE)) $WHERE .= " AND "." a.bl_no like '%".$blNo."%'";
	else $WHERE .= " a.bl_no like '%".$blNo."%'";	
	
if ($vessel_search)
   if (strlen($WHERE)) $WHERE .= " AND "." a.vessel like '%".$vessel_search."%'";
   else $WHERE .= " a.vessel like '%".$vessel_search."%'";	

if (strlen($WHERE))	$WHERE = " WHERE ".$conditionBaseAnd.$WHERE;
	else $WHERE = " WHERE ".$conditionBase." ";

$SQL = $SQL.$WHERE." ORDER BY lpj_id DESC";




//COUNT DATA

$WHERE = "";
$conditionBase = "";
$conditionBaseAnd = "";
$itCount = 1;


if($created_by == "master" || $group_login == "ADMIN")
{
    $conditionBase = " 1 ";
    $conditionBaseAnd = " 1 AND ";
}

else 
{
	if($user_type == "ADMIN")
	{
		if(!empty($userName))
		{
			$conditionBase = " 1 AND ( ";
			$conditionBaseAnd = " 1 AND ( ";
			
			foreach($userName as $idx => $val)
			{
			   
			   $conditionBase.= " a.staff_id = '".$val."' ";
			   if($itCount < $countUserName)
			   {
				   $conditionBase.= " OR ";
			   }
			   
			   $conditionBaseAnd.= " a.staff_id = '".$val."' ";
			   if($itCount < $countUserName)
			   {
				   $conditionBaseAnd.= " OR ";
			   }
			   			   			   
			   $itCount++;
			}
			$conditionBase.= " ) ";
			$conditionBaseAnd.= " ) ";
			$conditionBaseAnd.= " AND ";
	    }
	    
	    else
	    {
		    $conditionBase = " 1 ";
            $conditionBaseAnd = " 1 AND ";
		}
	}
	
	else
	{
		$conditionBase = " a.staff_id = '".$user_staff_id."' ";
		$conditionBaseAnd = " a.staff_id = '".$user_staff_id."' AND ";	
	}
}



$SQLCount = "SELECT count(*) FROM `$TableName` as a ";
$SQLCount.= "JOIN tbljoborder as b ON a.jo_id = b.jo_id ";
$SQLCount.= "JOIN tblproject as c ON b.project_id = c.project_id "; 
$SQLCount.= "JOIN ".$DBGeneral.".tblstaff as d ON b.staff_id = d.staff_id ";
$SQLCount.= "JOIN tbldebtur as e ON b.debtur = e.CUCODE ";


if ($jo_code) 
	if (strlen($WHERE)) $WHERE .= " AND "." a.jo_code like '%".$jo_code."%'";
	else $WHERE .= " a.jo_code like '%".$jo_code."%'";

if ($projName) 
	if (strlen($WHERE)) $WHERE .= " AND "." b.project_name like '%".$projName."%'";
	else $WHERE .= " b.project_name like '%".$projName."%'";
	
if ($blNo) 
	if (strlen($WHERE)) $WHERE .= " AND "." a.bl_no like '%".$blNo."%'";
	else $WHERE .= " a.bl_no like '%".$blNo."%'";
	
if ($vessel_search)
   if (strlen($WHERE)) $WHERE .= " AND "." a.vessel like '%".$vessel_search."%'";
   else $WHERE .= " a.vessel like '%".$vessel_search."%'";		

if (strlen($WHERE))	$WHERE = " WHERE ".$conditionBaseAnd.$WHERE;
	else $WHERE = " WHERE ".$conditionBase." ";

$SQLCount = $SQLCount.$WHERE." ORDER BY lpj_id DESC";




//Select Department 
//$SQLDept = "SELECT * FROM tbldept";

//******* SHOW FORM CONTENT *********
$PageNum = 1;
$RecCnt = $DBConnection->dbc->get_var($SQLCount) ; 

if (GetParam("PageNum","")) $PageNum  = GetParam("PageNum","");

$TBS = new clsTinyButStrong ;
$TBS->LoadTemplate($MOD_TEMPLATE_DIR."/".$FileTemplate);
$TBS->MergeBlock("bln",$KEY);
$TBS->MergeBlock("blg",$DBMysql,$SQL,$PageSize,$PageNum,$RecCnt);
//$TBS->MergeBlock("deptblk",$DBMysql,$SQLDept);
$TBS->MergeNavigationBar('nv',array('size'=>10,'pos'=>'centred'),$PageNum,$RecCnt,$PageSize) ; 
$TBS->Show();


function DeleteRecord ($sequence_name,$PageSize) {
	global $DBConnection,$FileName,$TableName;	
	for ($x=1;$x<=$PageSize;$x++) {				
		if (GetParam($sequence_name.$x,""))	{
			$DBConnection->Delete("$TableName",$sequence_name.$x);
	    }
	}
	header("Location: $FileName");
	exit;	
	
}

?>
